import { Modal, message } from 'ant-design-vue';

// Validate  表单验证
export function Validate (
  ref: string,
  successText = '操作成功！',
  errorText = '请检查所填内容是否有不符合规范的！'
) {
  return function (target: any, name: string, descriptor: any) {
    const oldMethod = descriptor.value;
    descriptor.value = function (...args: any[]) {
      const form = this.$refs[ref];
      form.validate((valid: boolean) => {
        if (valid) {
          oldMethod.call(this, ...args)
        } else {
          message.warning(errorText);
        }
      });
    };
    return descriptor;
  };
}

// 删除前提示
export function Confirm (
  content = '是否确定删除该信息？',
  preCheck?: any,
  successTest = '操作成功',
  title = '提示'
) {
  return function (target: any, name: string, descriptor: any) {
    const oldMethod = descriptor.value;
    descriptor.value = async function (...args: any[]) {
      try {
        let finalConfig = preCheck && await preCheck(...args)
        Modal.confirm({
          content,
          title,
          okText: '确定',
          cancelText: '取消',
          ...finalConfig,
          onOk: () => {
            oldMethod.call(this, ...args)
          },
          onCancel: () => {
            // message.warning("取消操作");
          }
        })
      } catch (error) {
        if (error.message !== 'noConfirm') {
          message.error(error.message)
        } else {
          oldMethod.call(this, ...args)
        }
      }
    };
    return descriptor;
  };
}
